#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../include/cJSON.h"
#include "../include/json_par.h"
#include "../include/logger.h"

int parse_json(const char *json_string,JsonData *json_struct) {
    
    cJSON *root, *source, *destination, *type, *cmd;
    // 解析 JSON 字符串并将其存储在 cJSON 根对象中
    root = cJSON_Parse(json_string);
    if (root == NULL) {
        writeLog(ERROR,__FILE__,__func__,"JSON parsing error\n");
        //printf("JSON parsing error\n");
        return 1;
    }

    // 从 JSON 根对象中获取各字段的值
    source = cJSON_GetObjectItem(root, "source");
    destination = cJSON_GetObjectItem(root, "destination");
    type = cJSON_GetObjectItem(root,"type");
    cmd = cJSON_GetObjectItem(root,"cmd");
   
    if (cJSON_IsNumber(source) && cJSON_IsNumber(destination) && cJSON_IsNumber(type) && cJSON_IsArray(cmd)) {
        // 将 JSON 字段的值存储到json_struct结构体中
        json_struct->source = source->valueint;
        json_struct->destination = destination->valueint;
        json_struct->type = type->valueint;

        // 获取 cmd 数组的长度
        size_t cmd_len = cJSON_GetArraySize(cmd);
        
        if(cmd_len > 0) {
            // 动态分配内存以存储 cmd 数组的数据
            json_struct->cmd = (char *)malloc(cmd_len * sizeof(unsigned char));

            if (json_struct->cmd == NULL) {
                // 内存分配错误处理
                writeLog(ERROR,__FILE__,__func__,"Memory allocation error\n");
                cJSON_Delete(root);
                return 1;
            }

            // 将 cmd 数组的数据复制到json_struct结构体中
            json_struct->cmd_len = cmd_len;
            
            for (size_t i = 0; i < cmd_len; i++) {
                cJSON *cmd_element = cJSON_GetArrayItem(cmd, i);
                if (cJSON_IsNumber(cmd_element)) {
                    json_struct->cmd[i] = (char)(cmd_element->valueint);
                }
            }

        } else {
             // 如果 cmd 数组为空，将 cmd 字段设置为 NULL
            json_struct->cmd = NULL;
            json_struct->cmd_len = 0;
        }

    } 

    // 释放 cJSON 根对象的内存
    cJSON_Delete(root);

    return 0;    
}


// 封装 JSON 数据的函数，返回 JSON 字符串
char* createAndPackageJsonData(int source, int destination, int type, int *dataArray, size_t arraySize) {
    cJSON *root = cJSON_CreateObject(); // 创建一个 JSON 对象

    // 添加整数数据到 JSON 对象中
    cJSON_AddNumberToObject(root, "source", source);
    cJSON_AddNumberToObject(root, "destination", destination);
    cJSON_AddNumberToObject(root, "type", type);

    // 创建 JSON 数组并添加到 JSON 对象中
    cJSON *dataArrayJson = cJSON_CreateArray();
    for (int i = 0; i < arraySize; i++) {
        cJSON_AddItemToArray(dataArrayJson, cJSON_CreateNumber(dataArray[i]));
    }
    cJSON_AddItemToObject(root, "cmd", dataArrayJson);

    char *jsonStr = cJSON_Print(root); // 将 JSON 对象转换为 JSON 字符串
    cJSON_Delete(root); // 释放 JSON 对象的内存

    return jsonStr; // 返回 JSON 字符串
}



#if 0
int main() {
    initLog("log.log");
    char *json_string = "{\"source\":3,\"destination\":1,\"type\":1,\"cmd\":[1,2,3,4]}";
    JsonData data;

    // 调用 parse_json 函数解析 JSON 数据
    int result = parse_json(json_string, &data);

    if (result == 0) {
        printf("JSON parsing succeeded:\n");
        printf("Source: %d\n", data.source);
        printf("Destination: %d\n", data.destination);
        printf("Type: %d\n", data.type);
        printf("Cmd: ");
        for (size_t i = 0; i < data.cmd_len; i++) {
            printf("0x%02X ", data.cmd[i]);
        }
        printf("\n");

        // 在成功解析后，释放动态分配的内存
        free(data.cmd);
    } else {
        printf("JSON parsing failed.\n");
    }

    return 0;
}

#endif



